﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Common.Logging;
using Jackie.Utility.CustomExtension;
using Jackie.Utility.Notify.Contracts;
using Jackie.Utility.Notify.Domain;

namespace Jackie.Utility.Notify.Client
{
    public partial class SendMessageForm : Form
    {
        private readonly ILog logger;

        public SendMessageForm()
        {
            InitializeComponent();
            logger = LogManager.GetLogger(GetType());
        }

        private void btnGetTitle_Click(object sender, EventArgs e)
        {
            try
            {
                using (var channelFacotry = new ChannelFactory<IPublish>("PublishService"))
                {
                    var clientProxy = channelFacotry.CreateChannel();
                    var titles = clientProxy.GetAllTitles();
                    titles.ForEach(item => cobTitle.Items.Add(item.Code));
                }
            }
            catch (Exception ex)
            {
                logger.Error("error!", ex);
            }
        }

        private void btnSendMessage_Click(object sender, EventArgs e)
        {
            try
            {
                var message = new MessageInfo()
                                  {
                                      Code = txtCode.Text,
                                      ExtenderMessage = txtExtender.Text,
                                      Message = txtContent.Text,
                                      TitleCode = cobTitle.SelectedItem.ToString()
                                  };
                logger.Info("start send message:" + message.ToString());
                using (var channelFactory = new ChannelFactory<IPublish>("PublishService"))
                {
                    var clientProxy = channelFactory.CreateChannel();
                    clientProxy.SendMessage(message);
                }
                logger.Info("end send message");
            }
            catch (Exception ex)
            {
                logger.Error("error!", ex);
            }
        }

        private void btnSendAllTime_Click(object sender, EventArgs e)
        {
            try
            {
                var message = new MessageInfo()
                                  {
                                      Code = txtCode.Text,
                                      ExtenderMessage = txtExtender.Text,
                                      Message = txtContent.Text,
                                      TitleCode = cobTitle.SelectedItem.ToString()
                                  };

                IList<TitleInfo> allTitles = null;

                using (var channelFactory = new ChannelFactory<IPublish>("PublishService"))
                {
                    var clientProxy = channelFactory.CreateChannel();
                    var titles = clientProxy.GetAllTitles();
                    allTitles = new List<TitleInfo>(titles);
                }


                var i = 1;
                while (true)
                {
                    var index = i % allTitles.Count;
                    message.TitleCode = allTitles[index].Code;
                    message.ExtenderMessage = DateTime.Now.ToString();
                    logger.Info("start send message:" + message.ToString());
                    using (var channelFactory = new ChannelFactory<IPublish>("PublishService"))
                    {
                        var clientProxy = channelFactory.CreateChannel();
                        clientProxy.SendMessage(message);
                    }
                    logger.Info("end send message");
                    logger.Info(string.Format("sleep {0} seconds", 30 * i));
                    Thread.Sleep(30000 * i);
                    logger.Info("sleep end!");
                    i++;
                }
            }
            catch (Exception ex)
            {
                logger.Error("error!", ex);
            }
        }
    }
}
